import {get} from "@utils/http"
import dayjs from "dayjs"
import { FormEvent } from "miniprogram/@types/Event"
// pages/myactivity/myactivity.js
import { remove } from "@utils/http";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTab:0,
    activityList:[]
  },
  async changeTab(e){
    this.setData({
      activeTab:e.detail.index
    })
    this.getActivity()
  },
  async getActivity(){
    wx.showLoading({
      title:"加载中..."
    })
    try{
      let url="/activity/self"
    let param=null
    switch(this.data.activeTab){
      case 0:
        url+="/sign"
        break
      case 1:
        param={passed:1}
        break
      case 2:
        param={passed:0}
        break
      case 3:
        param={passed:-1}
        break
    }
    const res=await get(url,param)
    this.setData({ 
      activityList:res?.map((item:any)=>{
        const start =dayjs(item.start)
        item.month=start.get("month")
        item.day=start.get("date")
        const diff=start.diff(dayjs(),"day")
        if(+diff==0){
          item.diff="今天"
        }else{
          if(+diff<0){
            item.diff="已结束"
          }else{
            item.diff=`还剩${diff}天`
          }
        }
        return item
      })
    })
    }catch{
      wx.showToast({
        title:"获取数据失败",
        icon:"none"
      })
      this.setData({
        activityList:[]
      })
    }finally{
      wx.hideLoading()
    }
  },
  deleteActivity(e:FormEvent<null>){
    this.setData({
      activityList:this.data.activityList.filter(item=>{
        return item.id!==e.detail.value
      })
    })
  },
  async cancelSignup(e:FormEvent<null>){
    this.setData({
      activityList:this.data.activityList.filter(item=>{
        return item.id!=e.detail.value
      })
    })
    await remove("/activity/signDown",{
      activityId:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getActivity()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})